﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entities;
using Datalayer;
using Common;

namespace Portal_KonTum.Admin
{
    public partial class DepartNew : System.Web.UI.Page
    {
        IQueryMgr iq;
        protected void Page_Load(object sender, EventArgs e)
        {
            Session["DepartID"] = null;
            Session["DepartID"] = Request.QueryString["DepartID"];

            if (!IsPostBack)
            {
                iq = QueryMgr.GetDbInstance();

                if (Session["DepartID"] == null)
                {

                }
                else
                {
                    bin_edit();
                    bt_save.Text = "Save";
                }
            }
        }

        protected void bt_save_Click(object sender, EventArgs e)
        {
            iq = QueryMgr.GetDbInstance();

            if (Session["DepartID"] == null)
            {
                // thêm mới
                iq.Dep_NewDepart(set_Cate());
                Response.Redirect("DepartMn.aspx?Notes=StrNew&Active=true");
            }
            else
            {
                // sua doi
                Guid gd = MethodCommon.Get_Guid(Session["DepartID"].ToString());
                iq.Dep_EditDepart(set_Cate(gd));
                Response.Redirect("DepartMn.aspx?Notes=StrEdit&Active=true");
            }

        }

        protected void bt_cancel_Click(object sender, EventArgs e)
        {

        }

        void Fill_DataEdit(Depart bg)
        {
            txt_name.Text = bg.DepartName;
            txt_link.Text = bg.Links;
        }

        void bin_edit()
        {
            Depart bg = new Depart();
            iq = QueryMgr.GetDbInstance();
            bg = iq.Dep_GetDepartByID(new Guid(Session["DepartID"].ToString()));
            Fill_DataEdit(bg);
        }

        /// <summary>
        /// Sửa chữa bug
        /// </summary>
        /// <param name="bg"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        Depart set_Cate(Guid id)
        {
            Depart bg = new Depart();

            if (id != null) //sửa chữa
                bg.DepartID = id;

            bg.DepartName = txt_name.Text;
            bg.Links = txt_link.Text;
            bg.IsDeleted = false;

            return bg;
        }

        /// <summary>
        /// thêm mới
        /// </summary>
        /// <param name="bg"></param>
        /// <returns></returns>
        Depart set_Cate()
        {
            Depart bg = new Depart();
            bg.DepartName = txt_name.Text;
            bg.Links = txt_link.Text;
            bg.IsDeleted = false;
            
            bg.DepartID = Guid.NewGuid();
            return bg;
        }
    }
}